using Scalar.AspNetCore;

namespace Digitalmes.WebApi.Extensions;

public static class OpenApiExtensions
{
    /// <summary>
    /// 使用 OpenApi 文件。
    /// </summary>
    /// <param name="app"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseDefaultOpenApi(this WebApplication app)
    {
        app.MapOpenApi();

        if (app.Environment.IsDevelopment())
        {
            app.MapScalarApiReference(options =>
            {
                // Disable default fonts to avoid download unnecessary fonts
                options.DefaultFonts = false;

                //options.WithHttpBasicAuthentication(basic =>
                // {
                //     basic.Username = "test";
                //     basic.Password = "your-password";
                // });
                //options.WithPreferredScheme("Bearer")
                //    .WithHttpBearerAuthentication(bearer =>
                //    {
                //        bearer.Token = ""; // 测试 Token
                //    });
            });
            app.MapGet("/", () => Results.Redirect("/scalar/v1")).ExcludeFromDescription();
        }

        return app;
    }
}
